package com.kibey.tools;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.nio.charset.Charset.defaultCharset;

/**
 * @author mchwind
 * @version V1.0
 * @since 18/9/11
 * 合并strings资源到一个文件
 * <p>
 * |       |    |   |   |````  |   |   ---  |\  |   |````
 * |      |_|   |   |   |  `|  |---|    |   | \ |   |  `|
 * |___  |   |   |_|     \_/   |   |   ___  |  \|    \_/
 */
public class MergeStringsFromApp {
    static String root = "/Users/mchwind/workspace/echo-android/Echo2.0";
    static File rootDir = new File(root);

    public static void main(String[] args) throws IOException {
        ArrayList<String[]> suffixList = new ArrayList<>();
        suffixList.add(new String[]{"values/", "strings.xml", "mitc"});
//        suffixList.add(new String[]{"values-en/", "strings.xml", "mitc"});
//        suffixList.add(new String[]{"values-ja-rJP/", "strings.xml", "mitc"});
//        suffixList.add(new String[]{"values-zh-rTW/", "strings.xml", "mitc"});
        for (String[] suffix : suffixList) {
            getFiles(suffix);
        }
    }

    public static File getStringsFile() throws IOException {
        return getFiles(new String[]{"values/", "strings.xml"});
    }


    public static File getFiles(String[] suffix) throws IOException {
        ArrayList<File> list = new ArrayList();
        getFiles(rootDir, list, suffix);
        File resultFile = new File(new File("").getAbsolutePath() + "/res/" + suffix[0] + suffix[1]);
        resultFile.delete();
        resultFile.getParentFile().mkdirs();
        resultFile.createNewFile();
        FileUtils.write(resultFile, "<resources>\n", defaultCharset(), true);

        for (File file : list) {
            String str = FileUtils.readFileToString(file, defaultCharset());
            str = str.replaceAll("<resources>", "");
            str = str.replaceAll("</resources>", "");
            FileUtils.write(resultFile, str, defaultCharset(), true);
        }

        FileUtils.write(resultFile, "</resources>", defaultCharset(), true);
        return resultFile;
    }

    private static void getFiles(File rootDir, List<File> list, String... suffix) {
        for (File file : rootDir.listFiles()) {
            if (file.isDirectory()) {
                getFiles(file, list, suffix);
            } else {
                if (contains(file.toString(), suffix)
                        && !file.getAbsolutePath().contains("iconfont_strings")
                        && (enableModule(file))) {
                    list.add(file);
                    logd(file);
                }
            }
        }
    }

    private static boolean enableModule(File file) {
        try {
            List<String> list = FileUtils.readLines(Utils.getResFile("modules"));
            for (String module : list) {
                if (file.toString().contains(module.trim() + "/src")) {
                    return true;
                }
            }
        } catch (IOException e) {
            return true;
        }
        return false;
    }

    private static boolean contains(String file, String[] suffix) {
        for (String s : suffix) {
            if (!file.contains(s)) {
                return false;
            }
        }
        return true;
    }

    public static void logd(Object file) {
        System.out.println(file);
    }
}
